﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Web.Services;
using System.Collections;
using Catering.View_Operaction;
using Catering_DBOpertion;

namespace Catering.yl.Daily
{
    public partial class DesignReview : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                LoadForm();
            }
        }
        protected void LoadForm()
        {
            MemList.Items.Clear();
            if (Session["ContractorPID"] != null)
            {
                //班组人员
                DataSet ds = SelMembersByCID(Session["ContractorPID"].ToString(), " and ClassID ='" + Session["ClassID"].ToString() + "' and GroupID ='" + Session["GroupID"].ToString() + "'");
                DataTable dt = ds.Tables[0];
                MemList.DataSource = dt;
                MemList.DataValueField = "id";
                MemList.DataTextField = "CName";
                MemList.DataBind();
                //方案内容
                DataTable dt_Plan = new Catering_Rules.ROCProject().GetList("ClassID =" + Session["ClassID"].ToString() + " and ContractorID =" + Session["ContractorPID"].ToString(),"").Tables[0];
                if( dt_Plan.Rows.Count > 0 )
                {
                    if (dt_Plan.Rows.Count != 0)
                    {
                        ProCompany.Text = dt_Plan.Rows[0]["ProCompany"].ToString();
                        ProName.Text = dt_Plan.Rows[0]["ProName"].ToString();
                        ProContent.Text = dt_Plan.Rows[0]["ProContent"].ToString();
                        Proschedule.Text = dt_Plan.Rows[0]["Proschedule"].ToString();
                    }
                    int strCount = 0;
                    for (int iCount = 0; iCount < dt_Plan.Rows[0]["PName"].ToString().Length; iCount++)
                    {
                        if (dt_Plan.Rows[0]["PName"].ToString()[iCount] == ';')
                            strCount++;
                    }
                    sel_list.Items.Clear();
                    sel_kahao.Items.Clear();
                    for (int selCount = 0; selCount < strCount; selCount++ )
                    {
                        ListItem newli = new ListItem();
                        string strTmp = dt_Plan.Rows[0]["PName"].ToString().Split(';')[selCount].ToString();
                        newli.Value = strTmp;
                        newli.Text = SingleMembersByID(strTmp).Tables[0].Rows[0][0].ToString();
                        sel_list.Items.Add(newli);
                        ListItem newkahao = new ListItem();
                        newkahao.Value = strTmp;
                        newkahao.Text = SingleMembersByID(strTmp).Tables[0].Rows[0][0].ToString() + "-" + GetPpaperByID(int.Parse(strTmp));
                        sel_kahao.Items.Add(newkahao);
                    }
                }
            }
        }
        protected void Button6_Click(object sender, EventArgs e)
        {
            CBL_ConMembers.Items.Clear();
            if (Session["ContractorPID"] != null)
            {
                DataSet ds = SelMembersByCID(Session["ContractorPID"].ToString(), " and ClassID = '' and GroupID = ''");
                DataTable dt = ds.Tables[0];
                CBL_ConMembers.DataSource = dt;
                CBL_ConMembers.DataValueField = "id";
                CBL_ConMembers.DataTextField = "CName";
                CBL_ConMembers.DataBind();
            }
            mainOpts.Visible = false;
            mainMember.Visible = true;
        }
        public DataSet SelMembersByCID(string strWhere, string str1)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,CName");
            strSql.Append(" FROM ContractorInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where CID =" + int.Parse(strWhere) + str1);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        public DataSet SingleMembersByID(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 CName");
            strSql.Append(" FROM ContractorInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where id =" + int.Parse(strWhere));
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            if (ProName.Text == "")
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", "alert('工程名称为空！')", true);
                return;
            }
            if (ProCompany.Text == "")
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", "alert('施工单位为空！')", true);
                return;
            }
            if (ProContent.Text == "")
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", "alert('具体内容为空！')", true);
                return;
            }
            if (Proschedule.Text == "")
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", "alert('施工进度为空！')", true);
                return;
            }
            if (sel_list.Items.Count == 0)
            {
                string strOpenUrl1 = "alert('请选择相应人员!')";
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", strOpenUrl1, true);
                return;
            }
             Catering_Model.ROCProject _roc = new Catering_Model.ROCProject();
             _roc.ProName = ProName.Text;
             _roc.ProCompany = ProCompany.Text;
             _roc.ProContent = ProContent.Text;

            string strPersonInfo = "";
            for (int i = 0; i < sel_list.Items.Count; i++ )
            {
                strPersonInfo += sel_list.Items[i].Value+";";
            }
            _roc.PName = strPersonInfo;
             _roc.Ppaper = "";
             _roc.Proschedule = Proschedule.Text;
            if (Session["ROCProject_ConID"] == null)
                return;
            else
                 _roc.ContractorID= Session["ROCProject_ConID"].ToString();
             if (Session["ClassID"] == null)
                return;
            else
                 _roc.ClassID= Session["ClassID"].ToString();
             if (Session["useDate"] == null)
                 return;
             else
                 _roc.AddDate = Convert.ToDateTime(Session["useDate"].ToString());
             if (Session["GroupID"] == null)
                 return;
             else
                 _roc.GroupID = Session["GroupID"].ToString();   

             new Catering_Rules.ROCProject().Add(_roc);
             string strOpenUrl = "alert('保存成功!')";
             ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", strOpenUrl, true);
        }
        public string GetPpaperByID(int id)
        {
            if (!string.IsNullOrEmpty(id.ToString()))
            {
                Catering_Model.ContratorMember cm = new Catering_Rules.ContratorMember().GetModel(id);
                if (cm != null)
                {
                    return cm.CardNo;
                }
            }
            return "&nbsp;";
        }

        protected void sel_selMember_Click(object sender, EventArgs e)
        {
            foreach (ListItem li in CBL_ConMembers.Items)
            {
                if (li.Selected)
                {
                    Edit(int.Parse(li.Value));
                }
            }
            MemList.Items.Clear();
            LoadForm();
            mainOpts.Visible = true;
            mainMember.Visible = false;
        }
        public DataSet SelMemberByCID(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 GroupID,ClassID");
            strSql.Append(" FROM ContractorInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where id=" + int.Parse(strWhere));
            }
            return DbHelperSQL.Query(strSql.ToString());
        }  
        public void Edit(int id)
        {
            Catering_Model.ContratorMember _cm = new Catering_Model.ContratorMember();
            _cm.Id = id;
            _cm.ClassID = Session["ClassID"].ToString();
            _cm.GroupID = Session["GroupID"].ToString();
            _cm.OldClassID = Session["ClassID"].ToString();
            _cm.OldGroupID = Session["GroupID"].ToString();
            _cm.ChangeClassDate = DateTime.Today;
            if (new Catering_Rules.ContratorMember().Update(_cm, ""))
            {
                string strResult = "alert('保存成功')";
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", strResult, true);
            }
        }

        protected void AddPerson_Click(object sender, EventArgs e)
        {
            //添加已选人员
            bool bFlag = true;
            for (int i = 0; i < sel_list.Items.Count; i++ )
            {
                if( MemList.SelectedValue == sel_list.Items[i].Value )
                    bFlag = false;
            }
            if(bFlag)
            {
                ListItem new_li = new ListItem();
                new_li.Value = MemList.SelectedValue;
                new_li.Text = MemList.SelectedItem.Text;
                sel_list.Items.Add(new_li);
                //添加已选人员卡号
                ListItem new_li_kahao = new ListItem();
                new_li_kahao.Value = MemList.SelectedValue;
                new_li_kahao.Text = MemList.SelectedItem.Text + "-" + GetPpaperByID(int.Parse(MemList.SelectedValue));
                sel_kahao.Items.Add(new_li_kahao);
            }
            else
            {
                string strError = "alert('该人员已添加，无须重复添加!')";
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", strError, true);
            }
        }

        protected void PlusPerson_Click(object sender, EventArgs e)
        {            
            sel_kahao.Items.RemoveAt(sel_list.SelectedIndex);
            sel_list.Items.RemoveAt(sel_list.SelectedIndex);
        }

        protected void outClass_Click(object sender, EventArgs e)
        {
            if (MemList.SelectedValue.ToString() == null || MemList.SelectedValue.ToString() == "")
            {
                string strOpenUrl = "alert('请选择相应人员!')";
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", strOpenUrl, true);
                return;
            }

            Catering_Model.ContratorMember _cm = new Catering_Model.ContratorMember();
            _cm.Id = int.Parse(MemList.SelectedValue);
            _cm.ClassID = "";
            _cm.GroupID = "";
            _cm.ChangeClassDate = DateTime.Today;
            if (new Catering_Rules.ContratorMember().Update(_cm, "", ""))
            {
                string strResult = "alert('移出班组成功')";
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", strResult, true);
            }
            MemList.Items.Clear();
            LoadForm();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ClientScript.RegisterStartupScript(this.GetType(),"", " <script> window.opener=null;window.top.close() </script> ");
        }
    }
}